// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import VueAwesomeSwiper from 'vue-awesome-swiper';
import 'swiper/dist/css/swiper.css';
import echarts from 'echarts';
import store from './store/store';
import axios from 'axios';

import { invalid, checkTime,checkInputValue} from "./config";
import VueBarcode from '@xkeshi/vue-barcode'; //导入条形码插件

Vue.component('barcode', VueBarcode);  //声明条形码组件

window.invalid = invalid;
window.checkTime = checkTime;
window.checkInputValue = checkInputValue;
Vue.prototype.$http = axios;
Vue.prototype.urls = 'http://192.168.21.185';

Vue.prototype.$echarts = echarts;
axios.defaults.withCredentials = true;
Vue.use(VueAwesomeSwiper);

Vue.use(ElementUI);

Vue.config.productionTip = false

let _this = this;
router.beforeEach((to, from, next) => {
  if (to.path == '/') {
    next()
  } else {
    if (localStorage.getItem('token')) {
      if (localStorage.getItem("time") - localStorage.getItem("nowTime") < 10 * 60) {
        let _this = this;
        let urls = "/api/adm/user/refreshToken/" + localStorage.getItem("uid") + "/" + localStorage.getItem("token");
        axios
          .post(urls, {
            refresh_token: localStorage.getItem("reToken")
          })
          .then(function (res) {
            let data = res.data;
            if (data.code == 1) {
              localStorage.setItem("token", data.data.user.token.access_token); //设置access_token
              localStorage.setItem("reToken", data.data.user.token.refresh_token); //设置refresh_token
              localStorage.setItem("time", data.data.user.token.expire_time); //记录token的有效时间
              localStorage.setItem("nowTime", new Date().getTime() / 1000); //记录当前登录的时间
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      } else {
        next();
      }
    } else {
      next({ path: '/' })
    }
  }
})
//让element的输入框自动获得焦点的自定义指令事件
// 
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

